import { reactive,watch } from "vue";
import {getData,setRate,AddMoney} from "@/api/article"
import { useRoute } from "vue-router"
import {message} from "ant-design-vue";
const history = (): any => {
    //列表数据
    const state= reactive({
      path:"user",
      columns:userColumns,//表格配置手册
      historyData: [],
      detailsData:[],//数据详情
      policeData:[],
      loading: false,
      switchs:true,
      visible2:false,//设置胜率弹框
      updateData:{uid:0,rate:0},
      Path:"/user/search",
      control:true,
      addMoney:1000,
      visible:false,//悬浮框
      historyParams: {  page_no: 0, page_size: 10,name:""},
      historyPagination: {
        total: 0,
        current: 1,
        pageSize: 10, //每页中显示10条数据
        showSizeChanger: true,
        pageSizeOptions: ["10", "15", "20", "30"], //每页中显示的数据
        showTotal: (total:any) => `共有 ${total} 条历史数据` //分页中显示总的数据
      },
    })
    
    const route = reactive(useRoute());
    watch(()=>route.path,(newPath,oldPath)=>{
        if(newPath.slice(0,5)==="/play"){
         state.loading=true
          state.Path=`${newPath.slice(5)}/search`
          if(newPath.slice(5)==="/user"){
            state.columns=userColumns
          }
          switch(newPath.slice(5)){
            case "/user":
              state.columns=userColumns
              break;
            case "/room":
             state.columns=columns
             break;
            case "/robot":
              state.columns=robotColumns
              break;
            case "/addmoney":
              state.columns=addmoneyColumns
              break;
            case "/recharge":
              state.columns=addmoneyColumns
              break;
            default :
            state.columns=columns
          }
           getHistory()
        }
    },)
  // 获取数据
  const getHistory=()=>{
   
    state.loading=true
    getData(`${state.Path}`,state.historyParams).then((res:any)=>{
      if(res.list.length>0){
      res.list.map((item:any)=>{
        if(item.time){
          item.time=formatDate(item.time)
        }
      })
      state.historyParams.name=""
     
      }
      state.historyData=res.list
      state.historyPagination.total=res.count*res.list.length
      state.historyPagination.pageSize=res.list.length
     
      state.loading=false
    }).catch((err)=>{
      state.historyData=[]
    })
  }
 //分页
  const historyChange=(pagination:any)=>{
    state.historyPagination.current = pagination.current;
    state.historyPagination.pageSize = pagination.pageSize;
    state.historyParams.page_no = pagination.current - 1;
    state.historyParams.page_size = pagination.pageSize;
    getHistory()
  }
 // 转换时间
  const  formatDate=(inputTime:number):string=>{
    if(!inputTime) return ""
    let date = new Date(inputTime);
    let y:any = date.getFullYear();
    let m:any = date.getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    let d:any = date.getDate();
    d = d < 10 ? "0" + d : d;
    let h:any = date.getHours();
    h = h < 10 ? "0" + h : h;
    let minute:any = date.getMinutes();
    let second:any = date.getSeconds();
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
  }
  /**
   * 悬浮框确定按钮
   */
  function handleOk():void{
    state.visible=false
    state.detailsData=[]
  }
  function details(men:any):void{
      state.detailsData=men
      state.visible=true
   
  }
  function setOpem(value:{uid:number,rate:number},control:boolean){
    state.control=control
    state.updateData.uid=value.uid
    if(control){
      state.updateData.rate=value.rate
    }
    state.visible2=true
  }
  function rateOk(){
    
   if(state.control){
    setRate(JSON.stringify(state.updateData)).then(res=>{
      console.log("请求成功");
      message.success("修改成功")
      state.visible2=false
      getHistory()
    })
   }else{
    AddMoney(JSON.stringify({uid:state.updateData.uid,money:state.addMoney})).then(res=>{
      message.success("加钱成功")
      state.visible2=false
      state.addMoney=1000
      getHistory()
    })
   }
  }
  getHistory();
  return {
    state, 
    getHistory,
    historyChange,
    formatDate,
    handleOk,
    details,
    setOpem,
    rateOk
  };
};
const columns = [
  {
    title: "ID",
    key: "id",
    dataIndex: "id",
    align: "center",
  },

  {
    title: "是否开局",
    key: "start",
    dataIndex: "start",
    align: "center",
  },
  {
    title: "轮数",
    key: "round",
    dataIndex: "round",
    align: "center",
  },
  {
    title: "底注",
    key: "bet",
    dataIndex: "bet",
    align: "center",
  },
  {
    title: "当前注数",
    key: "current",
    dataIndex: "current",
    align: "center",
  },
  {
    title: "人数(点击查看详情)",
    key: "mem",
    dataIndex: "mem",
    slots: { customRender: "mem" },
    align: "center",
  },
];
const userColumns=[
  {
    title: "名字",
    key: "name",
    dataIndex: "name",
    align: "center",
  },
  {
    title: "电话",
    key: "phone",
    dataIndex: "phone",
    align: "center",
  },
  {
    title: "余额",
    key: "money",
    dataIndex: "money",
    slots: { customRender: "money" },
    align: "center",
  },
  {
    title: "来源",
    key: "origin",
    dataIndex: "origin",
    align: "center",
  },
  {
    title:"注册时间",
    key: "register_time	",
    dataIndex: "register_time	",
    align: "center",
  },
  {
    title:"胜局",
    key: "win_num",
    dataIndex: "win_num",
    align: "center",
  },
  {
    title:"败局",
    key: "lose_num",
    dataIndex: "lose_num",
    align: "center",
  },
  {
    title:"胜率(%)",
    key: "rate",
    dataIndex: "rate",
    slots: { customRender: "rate" },
    align: "center",
  },
  // {
  //   title:"编辑",
  //   key: "uid",
  //   dataIndex: "uid",
  //   slots: { customRender: "uid" },
  //   align: "center",
  // }

]
const robotColumns=[
  {
    title: "uid",
    key: "uid",
    dataIndex: "uid",
    align: "center",
  },
  {
    title: "今日胜利局数",
    key: "win_num",
    dataIndex: "win_num",
    align: "center",
  },
  {
    title: "今日获胜金币",
    key: "win_money",
    dataIndex: "win_money",
    align: "center",
  },
  {
    title: "今日失败局数",
    key: "lose_num",
    dataIndex: "lose_num",
    align: "center",
  },
  {
    title: "今天胜利局数",
    key: "lose_money",
    dataIndex: "lose_money",
    align: "center",
  },

]
//加钱
const addmoneyColumns=[
  {
    title: "uid",
    key: "uid",
    dataIndex: "uid",
    align: "center",
  },
  {
    title: "名字",
    key: "name",
    dataIndex: "name",
    align: "center",
  },
  {
    title: "金额",
    key: "money",
    dataIndex: "money",
    align: "center",
  },
  {
    title: "时间",
    key: "time",
    dataIndex: "time",
    align: "center",
  },
  
  

]
// 充值记录
const recharge=[
  {
    title: "uid",
    key: "uid",
    dataIndex: "uid",
    align: "center",
  },
  {
    title: "名字",
    key: "name",
    dataIndex: "name",
    align: "center",
  },
  {
    title: "金额",
    key: "money",
    dataIndex: "money",
    align: "center",
  },
  {
    title: "时间",
    key: "time",
    dataIndex: "time",
    align: "center",
  },
]
export { history };
